//
//  FFFValueController.m
//  OneDiscountMall
//
//  Created by 樊凡凡 on 16/3/29.
//  Copyright © 2016年 樊凡凡. All rights reserved.
//

#import "FFFValueController.h"
#import "FFFValueHeaderModel.h"
#import "UIView+FFFView.h"

@interface FFFValueController () <UITableViewDataSource>
//存放tableView头图片的数组
@property(strong,nonatomic)NSMutableArray *headerArr;
//存放cell中模型的数组
@property(strong,nonatomic)NSMutableArray * dataSource;
@property(weak,nonatomic)UITableView * tableView;
@end

@implementation FFFValueController
-(NSMutableArray *)headerArr
{
    if (_headerArr==nil)
    {
        _headerArr=[NSMutableArray array];
        NSString *path=[[NSBundle mainBundle]pathForResource:@"Value.plist" ofType:nil];
        NSArray *arr=[NSArray arrayWithContentsOfFile:path];
        for (NSDictionary *dict in arr)
        {
            FFFValueHeaderModel *model=[FFFValueHeaderModel headerWithDict:dict];
            [_headerArr addObject:model];
        }
    }
    return _headerArr;
}
-(NSMutableArray *)dataSource
{
    if (_dataSource==nil)
    {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self createTableView];
}
-(void)createTableView
{
    UITableView *tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView=tableView;
    tableView.tableHeaderView=[self createHeaderView];
    [self.view addSubview:tableView];
    [tableView registerNib:[UINib nibWithNibName:@"FFFValueCell" bundle:nil] forCellReuseIdentifier:@"value"];
    tableView.rowHeight=self.view.bounds.size.width*0.3;
//    tableView.delegate=self;
    tableView.dataSource=self;
    
}
-(UIView *)createHeaderView
{
    //读取plist文件中的数组，根据数组创建按钮
    int margin=10;
    int btnW=(self.view.bounds.size.width-margin*3)*0.5;
    int btnH=self.view.bounds.size.width*0.2;
    int x=0;
    int y=0;
    int columns=2;
    UIView *headerView=[[UIView alloc]init];
    for (int i=0; i<self.headerArr.count; i++)
    {
        //得到第几行
        int row=i/columns;
        //得到第几列
        int column=i%columns;
        //x坐标
        x=margin+(margin+btnW)*column;
        //y坐标
        y=margin+(margin+btnH)*row;
        //创建button
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(x, y, btnW, btnH)];
        btn.tag=i;
        FFFValueHeaderModel *model=self.headerArr[i];
        [btn setImage:[UIImage imageNamed:model.img] forState:UIControlStateNormal];
        [headerView addSubview:btn];
    }
    headerView.width=self.view.width;
    headerView.height=(margin+btnH)*4;
    return headerView;
}

#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"value"];
    return cell;
}
@end
